package jp.co.sharp.android.parents.kidsguard.activity.common;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.json.JSONObject;

import jp.co.sharp.android.parents.kidsguard.App;
import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.setting.ContactOptActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.ContactOptAvatarActivity;
import jp.co.sharp.android.parents.kidsguard.common.DealWithWaitingDlg;
import jp.co.sharp.android.parents.kidsguard.common.ProcessWarningDlg;
import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestCodeConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestMethodConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestReceiverConstants;
import jp.co.sharp.android.parents.kidsguard.dto.BluetoothReceiverDto;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;
import jp.co.sharp.android.parents.kidsguard.thread.BusinessThread;


/**
 * 連絡先の画像を選択
 */
public class MimamoriContactAvatarActivity extends Activity {

    private ImageView rhino;
    private ImageView girafe;
    private ImageView bird;
    private ImageView dolphin;
    private ImageView elephant;
    private ImageView car;
    private ImageView train;
    private ImageView plane;
    private ImageView ship;
    private ImageView bike;
    private ImageView sweet;
    private ImageView icecream;
    private ImageView candy;
    private ImageView chocolate;
    private ImageView cupcake;
    private ImageView male1;
    private ImageView female1;
    private ImageView male2;
    private ImageView female2;
    private ImageView male3;
    private ImageView female3;
    private ImageView male4;
    private ImageView female4;
    private ImageView home;
    private ImageView school;
    private ImageView office;
    private ImageView default0;
    // 0: lock, 0: init , 1: getInfo , 2: setInfo, 3 :end
    private int mViewProcess = 0;
    private App mApp;
    private ParentBleTransfer mParentBleTransfer;
    private DealWithWaitingDlg mDealWithWaitingDlg;
    private KidInfoEntity mKidInfoEntity;

    private int mPageSerialNum;
    private Intent mIntent;

    private boolean mUnReceiver = true;
    // ページジャンプ
    private boolean mPageJumpFlag = false;
    // システムホームプレス
    private boolean mSystemHomeFlag = false;
    private static final String TAG = "TAG";
    // プログラムロック
    private String mSyncLock = "syncLock";
    // 編集を再開しています...
    private boolean mEditReopening = false;
    private static final int PAGE_EDIT_REOPEN = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "MimamoriContactAvatarActivity_onCreate");
        setContentView(R.layout.activity_mimamori_contact_avatar);

        mApp = (App)getApplication();
        initReceiver();
        initActivity();
        initListener();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "MimamoriContactAvatarActivity_onRestart");
        synchronized (mSyncLock) {
            if (mSystemHomeFlag) {
                if (mApp.getAppStatus() != Constants.APP_STATUS_USER_PRESENT) {
                    Message msg = new Message();
                    msg.what = PAGE_EDIT_REOPEN;
                    handler.sendMessage(msg);
                }
                mSystemHomeFlag = false;
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(this.TAG, "MimamoriContactAvatarActivity_onStart");
        mApp.setAppStatus(Constants.APP_STATUS_INIT);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "MimamoriContactAvatarActivity_onStop");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(systemReceiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
            }
            mUnReceiver = true;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "MimamoriContactAvatarActivity_onDestroy");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(systemReceiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
            }
            mUnReceiver = true;
        }
    }
    /**
     * 初期化Receiver
     */
    private void initReceiver() {
        Log.i(TAG, "MimamoriContactAvatarActivity_initReceiver");
        IntentFilter filter = new IntentFilter();
        // ロック画面
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        // 画面を開く
        filter.addAction(Intent.ACTION_USER_PRESENT);
        // ホームボタン
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED);
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED);
        registerReceiver(receiver, filter);
        registerReceiver(systemReceiver, filter);
        this.mParentBleTransfer = ((App)getApplication()).getmParentBleTransfer();
        mUnReceiver = false;
    }

    private void initActivity() {
        Log.i(TAG, "MimamoriContactAvatarActivity_initActivity");
        this.mIntent = getIntent();
        mPageSerialNum = mIntent.getIntExtra(Constants.KEY_PAGE_SERIAL_NUM,0);
        this.mKidInfoEntity = mApp.findKidInfoBySelect();

        rhino = (ImageView) findViewById(R.id.rhino);
        girafe = (ImageView) findViewById(R.id.girafe);
        bird = (ImageView) findViewById(R.id.bird);
        dolphin = (ImageView) findViewById(R.id.dolphin);
        elephant = (ImageView) findViewById(R.id.elephant);
        car = (ImageView) findViewById(R.id.car);
        train = (ImageView) findViewById(R.id.train);
        plane = (ImageView) findViewById(R.id.plane);
        ship = (ImageView) findViewById(R.id.ship);
        bike = (ImageView) findViewById(R.id.bike);
        sweet = (ImageView) findViewById(R.id.sweet);
        icecream = (ImageView) findViewById(R.id.icecream);
        candy = (ImageView) findViewById(R.id.candy);
        chocolate = (ImageView) findViewById(R.id.chocolate);
        cupcake = (ImageView) findViewById(R.id.cupcake);
        male1 = (ImageView) findViewById(R.id.male1);
        female1 = (ImageView) findViewById(R.id.female1);
        male2 = (ImageView) findViewById(R.id.male2);
        female2 = (ImageView) findViewById(R.id.female2);
        male3 = (ImageView) findViewById(R.id.male3);
        female3 = (ImageView) findViewById(R.id.female3);
        male4 = (ImageView) findViewById(R.id.male4);
        female4 = (ImageView) findViewById(R.id.female4);
        home = (ImageView) findViewById(R.id.home);
        school = (ImageView) findViewById(R.id.school);
        office = (ImageView) findViewById(R.id.office);
        default0 = (ImageView) findViewById(R.id.default0);
    }


    private void initListener() {
        Log.i(TAG, "MimamoriContactAvatarActivity_initListener");
        rhino.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("1");
                    return true;
                }
                return false;
            }
        });

        girafe.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("2");
                    return true;
                }
                return false;
            }
        });

        bird.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("3");
                    return true;
                }
                return false;
            }
        });


        dolphin.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("4");
                    return true;
                }
                return false;
            }
        });

        elephant.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("5");
                    return true;
                }
                return false;
            }
        });

        car.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("6");
                    return true;
                }
                return false;
            }
        });

        train.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("7");
                    return true;
                }
                return false;
            }
        });

        plane.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("8");
                    return true;
                }
                return false;
            }
        });

        ship.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("9");
                    return true;
                }
                return false;
            }
        });

        bike.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("10");
                    return true;
                }
                return false;
            }
        });

        sweet.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("11");
                    return true;
                }
                return false;
            }
        });

        icecream.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("12");
                    return true;
                }
                return false;
            }
        });
        candy.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("13");
                    return true;
                }
                return false;
            }
        });
        chocolate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("14");
                    return true;
                }
                return false;
            }
        });
        cupcake.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("15");
                    return true;
                }
                return false;
            }
        });
        male1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("16");
                    return true;
                }
                return false;
            }
        });
        female1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("17");
                    return true;
                }
                return false;
            }
        });
        male2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("18");
                    return true;
                }
                return false;
            }
        });
        female2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("19");
                    return true;
                }
                return false;
            }
        });
        male3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("20");
                    return true;
                }
                return false;
            }
        });
        female3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("21");
                    return true;
                }
                return false;
            }
        });
        male4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("22");
                    return true;
                }
                return false;
            }
        });
        female4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("23");
                    return true;
                }
                return false;
            }
        });
        home.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("24");
                    return true;
                }
                return false;
            }
        });
        school.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("25");
                    return true;
                }
                return false;
            }
        });
        office.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("26");
                    return true;
                }
                return false;
            }
        });
        default0.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    goToContactAddActivity("0");
                    return true;
                }
                return false;
            }
        });

    }

    /**
     * システム放送
     */
    private final BroadcastReceiver systemReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_SCREEN_OFF);
                        mViewProcess = Constants.SEND_KID_EXIT;
                        new BusinessThread(Constants.SEND_KID_EXIT,
                                mParentBleTransfer,
                                mKidInfoEntity,
                                null,
                                mPageSerialNum).start();
                    }
                } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_USER_PRESENT);
                        if (!mPageJumpFlag) {
                            Message msg = new Message();
                            msg.what = PAGE_EDIT_REOPEN;
                            handler.sendMessage(msg);
                        }
                    }
                } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_CLOSE_SYSTEM_DIALOGS);
                        mSystemHomeFlag = true;
                        mViewProcess = Constants.SEND_KID_EXIT;
                        new BusinessThread(Constants.SEND_KID_EXIT,
                                mParentBleTransfer,
                                mKidInfoEntity,
                                null,
                                mPageSerialNum).start();
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };

    /**
     * BroadcastReceiver
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    if (BluetoothAdapter.STATE_OFF == blueState) {
                        mKidInfoEntity.setBtConnStatus(Constants.DEVICE_DISCONNECTION_SUCCESS);
                        mApp.updateKidInfo(mKidInfoEntity);

                        String message = mKidInfoEntity.getKidName() + "と切断されました。編集を中断します。";
                        ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAvatarActivity.this, R.style.commonDlg, message);
                        processWarringDlg.show();
                        processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                            @Override
                            public void onOperateSet() {
                                processWarringDlg.dismiss();

                                backActivity(null);
                            }
                        });
                    }
                } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED.equals(action)) {
                    BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                    if (bluetoothReceiverDto.isFinished()) {
                        switch(mViewProcess) {
                            case Constants.SEND_KID_IS_USING:
                                break;
                            case Constants.SEND_KID_ENTER:
                                mEditReopening = false;
                                handler.sendEmptyMessage(0);
                                break;
                            case Constants.SEND_KID_GET_INFO:
                                break;
                            case Constants.SEND_KID_SET_INFO:
                                break;
                            case Constants.SEND_KID_EXIT:
                                break;
                        }
                    }
                } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED.equals(action)) {
                    BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                    String retStr = bluetoothReceiverDto.getRetStr();
                    JSONObject jsonObject = new JSONObject(retStr);
                     if (RequestMethodConstants.METHOD_ON_OPERATE_INTERRUPT.equals(jsonObject.getString("method"))) {
                        String message = "キッズケータイが操作されました。編集を中断します。";
                        ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAvatarActivity.this, R.style.commonDlg, message);
                        processWarringDlg.show();
                        processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                            @Override
                            public void onOperateSet() {
                                processWarringDlg.dismiss();
                                backActivity(null);
                            }
                        });
                    } else if (RequestMethodConstants.METHOD_GET_OPERATING_STATE.equals(jsonObject.getString("method"))) {
                        if (jsonObject.getJSONObject("data").getString("state").equals(Constants.IS_USING_OK)) {
                            mViewProcess = Constants.SEND_KID_ENTER;
                            new BusinessThread(Constants.SEND_KID_ENTER,
                                    mParentBleTransfer,
                                    mKidInfoEntity,
                                    null,
                                    mPageSerialNum).start();
                        } else {
                            handler.sendEmptyMessage(0);
                            String message = "別のスマートフォンから操作中のため、設定を変更できません。";
                            ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAvatarActivity.this, R.style.commonDlg, message);
                            processWarringDlg.show();
                            processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                                @Override
                                public void onOperateSet() {
                                    processWarringDlg.dismiss();
                                    backActivity(null);
                                }
                            });
                        }
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };

    /**
     * Dialog閉じる
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (0 == msg.what) {
                if (null != mDealWithWaitingDlg && mDealWithWaitingDlg.isShowing()) {
                    mDealWithWaitingDlg.dismiss();
                }
            } else if (msg.what == PAGE_EDIT_REOPEN) {
                if (!mEditReopening) {
                    // 非ページジャンプ編集が再び開きます
                    String message = "編集を再開しています...";
                    mDealWithWaitingDlg = new DealWithWaitingDlg(MimamoriContactAvatarActivity.this, R.style.commonDlg, message, false);
                    mDealWithWaitingDlg.show();
                    mViewProcess = Constants.SEND_KID_IS_USING;
                    new BusinessThread(Constants.SEND_KID_IS_USING,
                            mParentBleTransfer,
                            mKidInfoEntity,
                            null,
                            mPageSerialNum).start();
                    // ブロードキャストの再登録
                    unregisterReceiver(systemReceiver);
                    initReceiver();
                    mEditReopening = true;
                }
            }
        }
    };

    /**
     * ページリターンキー、フルスクリーンリターンジェスチャー
     */
    @Override
    public void onBackPressed() {
        backActivity(null);
    }

    /**
     * 前のページに戻る(値なし)
     */
    public void backActivity(View view) {
        Log.i(TAG, "MimamoriContactAvatarActivity_backActivity");
        if (null != view) {
            setResult(RequestCodeConstants.MIMAMORI_CONTACT_ADD_AVATAR, mIntent);
            // ページジャンプ
            mPageJumpFlag = true;
            finish();
        } else {
            mIntent.putExtra(Constants.KEY_PAGE_ERROR, "");
            setResult(RequestCodeConstants.MIMAMORI_CONTACT_ADD_AVATAR, mIntent);
            // ページジャンプ
            mPageJumpFlag = true;
            finish();
        }
    }

    private void goToContactAddActivity(String index) {
        Log.i(TAG, "MimamoriContactAvatarActivity_goToContactAddActivity");
        mIntent.putExtra(Constants.KEY_CONTACT_ADD_AVATAR, index);
        setResult(RequestCodeConstants.MIMAMORI_CONTACT_ADD_AVATAR, mIntent);
        // ページジャンプ
        mPageJumpFlag = true;
        finish();
    }
}
